<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="800" destroy-on-close @closed="$emit('closed')" :close-on-click-modal="false">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="right" class="flex_form">
			<!-- <el-form-item label="头像：">
				<sc-upload v-model="form.picture" title="上传头像"></sc-upload>
			</el-form-item> -->
			<el-form-item label="用户昵称：" prop="nickName" class="flex_item">
				<el-input v-model="form.nickName" placeholder="请输入用户昵称" clearable></el-input>
			</el-form-item>
			<el-form-item label="归属部门：" prop="deptId" class="flex_item">
				<el-cascader v-model="form.deptId" ref="myCascader" :options="groups" :props="groupsProps" :show-all-levels="false" placeholder="选择部门" @change="handleChanges" clearable ></el-cascader>
			</el-form-item>
			<el-form-item label="手机号码：" prop="phonenumber" class="flex_item">
				<el-input v-model="form.phonenumber" placeholder="请输入完手机号码" clearable></el-input>
			</el-form-item>
			<el-form-item label="邮箱：" prop="email"  class="flex_item">
				<el-input v-model="form.email" placeholder="请输入邮箱" clearable></el-input>
			</el-form-item>
			<template v-if="mode=='add'" >
				<el-form-item label="用户名称：" prop="userName" class="flex_item">
					<el-input v-model="form.userName" placeholder="请输入用户名称" clearable></el-input>
				</el-form-item>
			</template>
			<template v-if="mode=='add'" >
				<el-form-item label="用户密码：" prop="password" class="flex_item">
					<el-input type="password" placeholder="请输入用户密码" v-model="form.password" clearable show-password></el-input>
				</el-form-item>
			</template>
			<el-form-item label="性别：" prop="sex"  class="flex_item">
				<el-radio-group  v-model="form.sex">
					<el-radio :value="'0'">男</el-radio>
					<el-radio :value="'1'">女</el-radio>
				</el-radio-group>
			</el-form-item>
			<el-form-item label="状态：" prop="status"  class="flex_item">
				<el-radio-group  v-model="form.status">
					<el-radio :value="'0'">正常</el-radio>
					<el-radio :value="'1'">停用</el-radio>
				</el-radio-group>
			</el-form-item>
			<el-form-item label="岗位：" prop="postIds" class="flex_item" >
				<el-select v-model="form.postIds" multiple filterable >
					<el-option v-for="item in postOptions" :key="item.postId" :label="item.postName" :value="item.postId" :disabled="item.status == 1"/>
				</el-select>
			</el-form-item>
			<el-form-item label="角色：" prop="roleIds" class="flex_item" >
				<el-select v-model="form.roleIds" multiple filterable >
					<el-option v-for="item in roleOptions" :key="item.roleId" :label="item.roleName" :value="item.roleId" :disabled="item.status == 1"/>
				</el-select>
			</el-form-item>
			<el-form-item label="备注：" prop="remark">
				<el-input  v-model="form.remark" type="textarea" ></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
	import { verifyPhone } from '@/utils/verificate'
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增用户',
					edit: '编辑用户',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {},
				groupsProps: {
					value: "id",
					label: 'label',
					checkStrictly: true,
					expandTrigger: 'hover'
				},

				roleGroups: [],
				//验证规则
				rules: {
					nickName: [
						{required: true, message: '请输入登录账号'}
					],
					userName: [
						{required: true, message: '请输入用户名称'}
					],
					password: [
						{ required: true, message: '请输入密码'},
						{min: 6, message: '长度需为6位'}
					],

					email: [
						{
							type: "email",
							message: "请输入正确的邮箱地址",
							trigger: ["blur"]
						}
					],
				},
				groups: [], // 归属部门
				postOptions: [], // 岗位
				roleOptions: [], // 角色
			}
		},
		mounted() {
			this.getGroup()
		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.reset()
				this.visible = true;
				return this
			},
			//加载部门数据
			async getGroup(){
				var res = await this.$API.auth.deptTreeSelect.get();
				this.groups = res.data;
			},
			//筛选搜索
     remoteMethod(query) {
				setTimeout(() => {
					this.getRoleGroup(query)
				}, 200);
     },
			//加载权限数据
			async getRoleGroup(query){
				let keyword = query || ''
				var res = await this.$API.common.getAllRoleList.get({
					keyword,
					limit: 100
				});
				this.roleGroups = res.data;
			},
			// 选择岗位
			handleChanges(e) {
				this.$nextTick(() => {
					let deptId = e.length == 1 ? e[0] : e[e.length -1] // 选择1级拿当前， 选择多级拿数组最后一个id
					this.form.deptId = deptId
					this.$refs.myCascader.togglePopperVisible(false)
				})
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						let mode = this.mode
						let form = this.form
						var res = mode == 'add' ? await this.$API.auth.editUser.add(form) : await this.$API.auth.editUser.edit(form)
						this.isSaveing = false;
						if(res.code == 200){
							this.$emit('success')
							this.visible = false;
							this.$message.success("操作成功")
						}
					}
				})
			},
			//表单注入数据
			async setData(data){
				let userId = data ?  data.userId : ''
				var res = await this.$API.auth.getUserDetail.get({userId});
				if(res.code == 200) {
					this.postOptions = res.posts
					this.roleOptions = res.roles
					if(this.mode == 'edit') {
						let info = res.data
						info.postIds = res.postIds
						info.roleIds = res.roleIds
						this.form = info
					}
				}
			},
			// 表单重置
			reset() {
				this.form = {
					userId: undefined,
					deptId: undefined,
					userName: undefined,
					nickName: undefined,
					password: undefined,
					phonenumber: undefined,
					email: undefined,
					sex: '0',
					status: "0",
					remark: undefined,
					postIds: [],
					roleIds: []
				}
			},
		}
	}
</script>

<style>
</style>
